#include "oeinputbox_.h"

#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QRegExpValidator>
#include <QDebug>
#include <QKeyEvent>

// private lib
#include <OEMessageBox>

OEInputBox_::OEInputBox_(QWidget *parent) : QDialog(parent)
{
    setWindowTitle(tr("验证密码"));
    raise();
    setFixedSize(400,200);

    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_input = new QHBoxLayout();
    labInput_ = new QLabel(tr("密码: "));
    editInput_ = new QLineEdit();
    btnConfirm_ = new QPushButton(tr("确认"));
    QPushButton* btn_close = new QPushButton("Χ");
    btn_close->setObjectName("CloseButton");


    // size
    labInput_->setFixedSize(80,40);
    editInput_->setFixedSize(200,40);
    btn_close->setFixedSize(35,35);
    btnConfirm_->setFixedSize(100,35);



    // 输入限制规则
    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quote_validator = new QRegExpValidator(regQuote, this);
    editInput_->setValidator(quote_validator);

    editInput_->setPlaceholderText(tr("请填写密码"));
    editInput_->setEchoMode(QLineEdit::EchoMode::Password);


    // link
    connect(btn_close, &QPushButton::clicked,
            this, &OEInputBox_::close);
    connect(btnConfirm_, &QPushButton::clicked,
            this, [=]{
        if (editInput_->text().isEmpty()) {
            OEMessageBox::BriefMessage(this, "输入不能为空");
            return;
        }

        emit this->sigInputMsg(editInput_->text());
        close();
    });


    layout_input->addWidget(labInput_);
    layout_input->addWidget(editInput_);

    layout->addWidget(btn_close,0,Qt::AlignTop | Qt::AlignRight);
    layout->addLayout(layout_input,20);
    layout->addWidget(btnConfirm_,0,Qt::AlignCenter);

    layout->setMargin(0);

#if 1
    QLabel* lab_background = new QLabel(this);
    lab_background->setStyleSheet("border: 1px solid rgb(0,0,0);");
    lab_background->setFixedSize(size());
    lab_background->lower();

    setWindowFlags(Qt::ToolTip);
    setLayout(layout);
#else
    QWidget* main_wdgt = new QWidget();
    main_wdgt->setLayout(layout);
    setCentralWidget(main_wdgt);
#endif
    editInput_->setFocus();
}

void OEInputBox_::setEditEchoMode(QLineEdit::EchoMode _mode)
{
    if (_mode == QLineEdit::EchoMode::Password) {
//        connect(btnConfirm_, &QPushButton::clicked,
//                this, [=]{
//            if (editInput_->text().isEmpty()) {
//                OEMessageBox::BriefMessage(this, "输入不能为空");
//                return;
//            }
//            if (editInput_->text().length() < 6) {
//                OEMessageBox::BriefMessage(this, "密码不能小于6位");
//                return;
//            }

//            emit this->sigInputMsg(editInput_->text());
//            close();
//        });
    }
    else
        editInput_->setEchoMode(_mode);

}

void OEInputBox_::setEditLabel(const QString &_title)
{
    labInput_->setText(_title);
}

void OEInputBox_::setEditValidator(const QString &_regExp) {
    QRegExp regQuote(_regExp);
    QRegExpValidator* quote_validator = new QRegExpValidator(regQuote, this);
    editInput_->setValidator(quote_validator);
}

void OEInputBox_::setEditPlaceholderText(const QString &_text)
{
    editInput_->setPlaceholderText(_text);
}



void OEInputBox_::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
        emit btnConfirm_->clicked();
        event->accept();
    }
    else if (event->key() == Qt::Key_Escape) {
        QLineEdit* edit = qobject_cast<QLineEdit*>(this->focusWidget());
        if (edit == Q_NULLPTR)
            return;

        edit->clear();
        event->accept();
    }
}
